package io.ukoko.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.ukoko.dto.Page;
import io.ukoko.entity.Book;
import io.ukoko.mapper.BookMapper;
import io.ukoko.service.BookService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.xml.crypto.Data;
import java.util.Date;
import java.util.List;

@Transactional
@Service
public class BookServiceImpl implements BookService {
    @Resource
    private BookMapper bookMapper;
    @Override
    public void addBook(Book book) {
        bookMapper.addBook(book);
    }

    @Override
    public void deleteBookByBookId(Integer bookId) {
        bookMapper.deleteBookByBookId(bookId);
    }

    @Override
    public void batchDeleteBook(List<Integer> bookIds) {
        bookMapper.batchDeleteBook(bookIds);
    }

    @Override
    public void updateBook(Book book) {
        bookMapper.updateBook(book);
    }

    @Override
    public Page getBooksPage(Integer pageNo, Integer pageSize, String bookName, Integer categoryId, Integer status, Date start, Date end) {
        Page page = new Page();
        PageHelper.startPage(pageNo,pageSize);
        List<Book> books = bookMapper.getBooksByParams(bookName, categoryId, status, start, end);
        PageInfo<Book> info = new PageInfo<>(books);
        page.setPage(info.getList());
        page.setPageNo(info.getPageNum());
        page.setPageSize(info.getPageSize());
        page.setHasNext(info.isHasNextPage());
        page.setHasPre(info.isHasPreviousPage());
        page.setPageCount(info.getPages());
        page.setTotal(info.getTotal());
        return page;
    }


}
